//
//  RootViewController.m
//  UINavigationDemo
//
//  Created by shk on 14/11/11.
//  Copyright (c) 2014年 com.shk. All rights reserved.
//

#import "RootViewController.h"
#import "SecondViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController


- (void) loadView{
    UIView *baseView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    baseView.backgroundColor = [UIColor purpleColor];
    self.view = baseView;
    self.title = @"root";
    
    
    /**
     * 一个导航控制器控制着若干个视图控制器
     * 一个导航控制器包含一个NavigationBar和一个toolBar
     * NavigationBar中的"按钮"是一个UINavigationItem(only one)
     * 通过设置UINavigationItem的属性，显示Item(UINavigationBar)
     * UINavigationItem不是由navigationBar控制，更不由UINavigationController控制，而是由当前的视图控制器控制
     */
     
     
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setTitle:@"Push" forState:UIControlStateNormal];
    [button setFrame:CGRectMake(90, 100, 140, 35)];
    [button addTarget:self action:@selector(pushVC) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(study)];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    /**
     * 头部
     */
    UIButton *rightItemBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [rightItemBtn setTitle:@"test" forState:UIControlStateNormal];
    [rightItemBtn setFrame:CGRectMake(90, 100, 90, 35)];
    [rightItemBtn addTarget:self action:@selector(test) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightItemBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    titleView.backgroundColor = [UIColor yellowColor];
    self.navigationItem.titleView = titleView;
    
    
    /**
     * 工具栏
     */
    /*
    UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 460-44-44, 320, 44)];
    toolBar.barStyle = UIBarStyleDefault;
    [self.view addSubview:toolBar];
    */
     
    UIBarButtonItem *addItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:nil];
    UIBarButtonItem *saveItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:nil];
    UIBarButtonItem *titleItem = [[UIBarButtonItem alloc] initWithTitle:@"title" style:UIBarButtonItemStylePlain  target:self action:nil];
    UIBarButtonItem *imageItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"apple_red"] style:UIBarButtonItemStylePlain target:self action:nil];
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"apple_red"]];
    UIBarButtonItem *imageViewItem = [[UIBarButtonItem alloc] initWithCustomView:imageView];
    //设置间隔
    UIBarButtonItem *flexibleItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
    flexibleItem.width = 20;
    NSArray *items = @[addItem, flexibleItem, saveItem, flexibleItem, titleItem, imageItem, imageViewItem];
//    [toolBar setItems:items animated:YES];
    
    [self.navigationController setToolbarHidden:NO animated:NO];
    [self setToolbarItems:items animated:YES];
    
    
}

- (void) pushVC{
    SecondViewController *secondVC = [[SecondViewController alloc] init];
    [self.navigationController pushViewController:secondVC animated:YES];

}

- (void) study{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"study" message:@"恭喜" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alertView show];
}

- (void) test{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"study" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"确定", nil];
    [actionSheet showInView:self.view];
    
}


- (void) viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self.navigationController setToolbarHidden:NO animated:YES];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
